package com.wgchao.mdmp.service.impl;

import com.wgchao.mdmp.api.GrxxHttp;
import com.wgchao.mdmp.common.ResultCommon;
import com.wgchao.mdmp.dao.*;
import com.wgchao.mdmp.entity.*;
import com.wgchao.mdmp.mapstruct.dto.GrYlData;
import com.wgchao.mdmp.mapstruct.dto.MdmpGrxxDTO;
import com.wgchao.mdmp.mapstruct.mapper.*;
import com.wgchao.mdmp.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.Date;
import java.util.List;

/**
 * @作者：wgchao
 * @时间：19:48 2019/11/3 0003
 * @用途：
 **/
@Service
@Transactional
public class DataServiceImpl implements DataService {
    @Autowired
    private GrxxHttp grxxHttp;
    @Autowired
    private GxyDao gxyDao;
    @Autowired
    private MdmpGxyMapper gxyMapper;
    @Autowired
    private XtDao xtDao;
    @Autowired
    private MdmpXtMapper xtMapper;
    @Autowired
    private TwDao twDao;
    @Autowired
    private MdmpTwMapper twMapper;
    @Autowired
    private XlDao xlDao;
    @Autowired
    private MdmpXlMapper xlMapper;
    @Autowired
    private HxDao hxDao;
    @Autowired
    private MdmpHxMapper hxMapper;
    @Autowired
    private Sao2Dao sao2Dao;
    @Autowired
    private MdmpSao2Mapper sao2Mapper;

    /**
     * 查询患者检测数据
     * @param idcard 身份证号
     * @param startTime 开始检测时间
     * @param endTime 结束检测时间
     * @param types 待获取的数据类型
     * @return
     * @throws Exception
     */
    @Override
    public ResultCommon<GrYlData> searchData(String idcard, Date startTime, Date endTime, String types) throws Exception {
        //获取患者信息
        MdmpGrxxDTO grxxDTO = grxxHttp.searchPatientInfo(idcard);
        if(grxxDTO == null){
            return ResultCommon.fail("患者信息不存在");
        }
        if(grxxDTO.isDel()){
            return ResultCommon.fail("患者信息已删除");
        }

        GrYlData grYlData = new GrYlData();
        grYlData.setGrxxInfo(grxxDTO);

        //查询血压
        if(StringUtils.isEmpty(types) || types.contains("xy")){
            List<MdmpGxy> gxyList = gxyDao.searchRecords(idcard, startTime, endTime);
            grYlData.setXyInfos(gxyMapper.toDTO(gxyList));
        }

        //查询血糖
        if(StringUtils.isEmpty(types) || types.contains("xt")){
            List<MdmpXt> xtList = xtDao.searchRecords(idcard, startTime, endTime);
            grYlData.setXtInfos(xtMapper.toDTO(xtList));
        }

        //查询体温
        if(StringUtils.isEmpty(types) || types.contains("tw")){
            List<MdmpTw> twList = twDao.searchRecords(idcard, startTime, endTime);
            grYlData.setTwInfos(twMapper.toDTO(twList));
        }

        //查询心率
        if(StringUtils.isEmpty(types) || types.contains("xl")){
            List<MdmpXl> xlList = xlDao.searchRecords(idcard, startTime, endTime);
            grYlData.setXlInfos(xlMapper.toDTO(xlList));
        }

        //查询呼吸频率
        if(StringUtils.isEmpty(types) || types.contains("hx")){
            List<MdmpHx> hxList = hxDao.searchRecords(idcard, startTime, endTime);
            grYlData.setHxInfos(hxMapper.toDTO(hxList));
        }

        //查询SAO2
        if(StringUtils.isEmpty(types) || types.contains("sao2")){
            List<MdmpSao2> sao2List = sao2Dao.searchRecords(idcard, startTime, endTime);
            grYlData.setSao2Infos(sao2Mapper.toDTO(sao2List));
        }
        return ResultCommon.success().setData(grYlData);
    }


}
